/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.paging;

import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.annotation.AnGlobal;
import org.zhiqim.kernel.util.Validates;

/**
 * 只有上一页&下一页的分页显示，支持显示如下：<br>
 * 1、上一页<br>
 * 2、下一页<br>
 *
 * @version v1.0.0 @author zouzhigang 2017-11-16 新建与整理
 */
@AnAlias("PagingUD")
@AnGlobal
public class PagingUD implements Paging
{
    /**
     * 提供传入的是URL链接的分页显示
     * 
     * @param result        结果集
     * @return              分页显示HTML
     */
    public String toHtmlLink(PageResult<?> result)
    {
        return toHtmlLink(result, null);
    }
    
    /**
     * 提供传入的是URL链接的分页显示
     * 
     * @param result        结果集
     * @param link          URL链接
     * @return              分页显示HTML
     */
    public String toHtmlLink(PageResult<?> result, String link)
    {
        if (result == null)
            result = PageBuilder.newResult(20);
        
        if (link == null)
            link = "";
        
        String condition = result.getConditionUrl();
        if (Validates.isNotEmpty(condition))
            link += condition + "&";
        else
            link += "?";
        
        StringBuilder strb = new StringBuilder();
        strb.append("<div class=\"z-relative-center z-text-center\">");
        
        if (result.hasPrevious())
            strb.append("<span class=\"z-button z-mg-r3\" onclick=\"location.href='" + link + "page=" + (result.pageNo() - 1) + "'\">上一页</span>");
        else
            strb.append("<span class=\"z-button z-mg-r3 z-readonly\">上一页</span>");
    
        if (result.hasNext())
            strb.append("<span class=\"z-button z-mg-r3\" onclick=\"location.href='" + link + "page=" + (result.pageNo() + 1) + "'\">下一页</span>");
        else
            strb.append("<span class=\"z-button z-mg-r3 z-readonly\">下一页</span>");
    
        strb.append("</div>");
        return strb.toString();
    }
    
    /**
     * 提供传入的是Js函数名的分页显示
     * 
     * @param result        结果集
     * @param jsMethod      Js函数
     * @return              分页显示HTML
     */
    public String toHtmlClick(PageResult<?> result, String jsMethod)
    {
        return toHtmlClick(result, jsMethod, null, null);
    }
    
    /**
     * 提供传入的是Js函数名的分页显示
     * 
     * @param result        结果集
     * @param cls           类名
     * @param               方法
     * @return              分页显示HTML
     */
    public String toHtmlClick(PageResult<?> result, String cls, String method)
    {
        return toHtmlClick(result, null, cls, method);
    }
    
    /** 提供传入的是Js函数名或类和方法的分页显示 */
    private String toHtmlClick(PageResult<?> result, String jsMethod, String cls, String method)
    {
        if (result == null)
            result = PageBuilder.newResult(20);
        
        String click = (jsMethod == null)?("Z.ajaxq('" + cls + "', '" + method + "', "):(jsMethod + "(");
        
        StringBuilder strb = new StringBuilder();
        strb.append("<div class=\"z-relative-center z-text-center\">");
        
        if (result.hasPrevious())
            strb.append("<span class=\"z-button z-mg-r3\" onclick=\""+ click  + (result.pageNo() - 1) + ");\">上一页</span>");
        else
            strb.append("<span class=\"z-button z-mg-r3 z-readonly\">上一页</span>");
    
        if (result.hasNext())
            strb.append("<span class=\"z-button z-mg-r3\" onclick=\""+ click + (result.pageNo() + 1) + ");\">下一页</span>");
        else
            strb.append("<span class=\"z-button z-mg-r3 z-readonly\">下一页</span>");
    
        strb.append("</div>");
        return strb.toString();
    }
}
